package com.zmc.lostfound.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmc.lostfound.auth.dao.RoleDao;
import com.zmc.lostfound.auth.entity.RoleEntity;
import com.zmc.lostfound.auth.service.RolePermissionService;
import com.zmc.lostfound.auth.service.RoleService;
import com.zmc.lostfound.auth.service.UserRoleService;
import com.zmc.lostfound.auth.vo.RoleSaveVO;
import com.zmc.lostfound.common.exception.MyException;
import com.zmc.lostfound.common.utils.PageParams;
import com.zmc.lostfound.common.utils.PageResult;
import com.zmc.lostfound.common.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ZMC
 * @description 角色信息管理业务类
 * @email 2869581855@qq.com
 * @date 2023-07-05 15:17:37
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {

    private final UserRoleService userRoleService;

    private final RolePermissionService rolePermissionService;

    public RoleServiceImpl(UserRoleService userRoleService, RolePermissionService rolePermissionService) {
        this.userRoleService = userRoleService;
        this.rolePermissionService = rolePermissionService;
    }

    /**
     * 分页模糊查询角色信息
     *
     * @param pageParams 分页参数
     * @return PageResult
     */
    @Override
    public PageResult queryPage(PageParams pageParams) {
        Integer pageNo = pageParams.getPageNo();
        Integer pageSize = pageParams.getPageSize();
        String keyword = pageParams.getKeyword();
        Page<RoleEntity> page = new Page<>(pageNo, pageSize);
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword)) {
            queryWrapper.eq("id", keyword)
                    .or()
                    .eq("role_code", keyword)
                    .or()
                    .like("role_name", keyword)
                    .or()
                    .like("role_code", keyword);
        }
        return new PageResult(this.page(page, queryWrapper));
    }

    /**
     * 按id查询角色信息
     *
     * @param id 角色id
     * @return RoleEntity
     */
    @Override
    public RoleEntity queryInfoById(Long id) {
        if (id == null || id <= 0) {
            MyException.throwException("查询失败: 角色id不能为null且必须大于0");
        }
        return this.getById(id);
    }

    /**
     * 按用户id查询角色信息
     *
     * @param userId 用户id
     * @return List<RoleEntity>
     */
    @Override
    public List<RoleEntity> queryInfoByUserId(Long userId) {
        if (userId == null || userId <= 0) {
            MyException.throwException("查询失败: 用户id不能为null且必须大于0");
        }
        return this.baseMapper.queryInfoByUserId(userId);
    }

    /**
     * 按权限id查询角色信息
     *
     * @param permissionId 权限id
     * @return List<RoleEntity>
     */
    @Override
    public List<RoleEntity> queryInfoByPermissionId(Long permissionId) {
        if (permissionId == null || permissionId <= 0) {
            MyException.throwException("查询失败: 权限id不能为null且必须大于0");
        }
        return this.baseMapper.queryInfoByPermissionId(permissionId);
    }

    /**
     * 新增角色信息
     *
     * @param roleSaveVO 角色信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result insertRole(RoleSaveVO roleSaveVO) {
        RoleEntity roleEntity = BeanUtil.copyProperties(roleSaveVO, RoleEntity.class);
        if (!this.save(roleEntity)) {
            MyException.throwException("新增角色信息失败");
        }
        return Result.ok("新增成功");
    }

    /**
     * 修改角色信息
     *
     * @param roleSaveVO 角色信息新增/修改请求体
     * @return Result
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result updateRole(RoleSaveVO roleSaveVO) {
        RoleEntity roleEntity = BeanUtil.copyProperties(roleSaveVO, RoleEntity.class);
        if (!this.updateById(roleEntity)) {
            MyException.throwException("修改角色信息失败");
        }
        return Result.ok("修改成功");
    }

    /**
     * 按角色id删除角色信息
     *
     * @param roleId 角色id
     * @return Result
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Result deleteByRoleId(Long roleId) {
        if (roleId == null || roleId <= 0) {
            MyException.throwException("删除失败: 角色id不能为null且必须大于0");
        }
        // 1.按角色id删除用户&角色关联信息
        userRoleService.deleteByUserIdAndRoleId(null, roleId);
        // 2.按角色id删除角色&权限关联信息
        rolePermissionService.deleteByRoleIdAndPermissionId(roleId, null);
        // 3.删除该角色信息
        this.removeById(roleId);
        return Result.ok("删除成功");
    }

}